package com.xinfu.newproject.widget.picture;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.xinfu.newproject.R;
import com.xinfu.newproject.widget.photoview.PhotoView;

import java.util.Arrays;
import java.util.List;

public class ViewPagerActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        ViewPager viewPager = findViewById(R.id.view_pager);
        String bundle = getIntent().getExtras().getString("picture");
        String[] imageList = bundle.split(";");
        List<String> ss = Arrays.asList(imageList);
        viewPager.setAdapter(new SamplePagerAdapter(this, ss));
        viewPager.setCurrentItem(getIntent().getExtras().getInt("position"));
    }

    static class SamplePagerAdapter extends PagerAdapter {

        List<String> sDrawables;
        Context context;

        public SamplePagerAdapter(Context context, List<String> sDrawables) {
            this.sDrawables = sDrawables;
            this.context = context;
        }

        @Override
        public int getCount() {
            return sDrawables == null ? 0 : sDrawables.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                photoView.setTransitionName("image" + position);
            }
            Glide.with(context).load(sDrawables.get(position)).asBitmap().into(photoView);
            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

}
